﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;
using DflyingForum.Core.Repositories;
using DflyingForum.Core.Data.Domain.Entities;
using DflyingForum.Core.Framework.DflyingPortal;

namespace Core.Web.Code.UI
{
    /// <summary>
    ///AppSettingPage 的摘要说明
    /// </summary>
    public class AppSettingPage
    {
        //页面实际访问的URL
        public string Url { get; set; }

        //模板页面加载控件的路径		
        public string TeamplateFilePath { get; set; }

        /// <summary>
        /// 根据当前请求的URL初始化应控件的路径
        /// </summary>
        /// <param name="url"></param>
        public AppSettingPage(string url)
        {
            this.Url = url.ToLower();
            AnalysisUrl(this.Url);
        }

        /// <summary>
        /// 根据当前请求初始化应控件的路径
        /// </summary>
        /// <param name="context"></param>
        public AppSettingPage(HttpContext context)
        {
            string url = context.Request.Url.ToString();
            this.Url = url.ToLower();
            AnalysisUrl(this.Url);
        }

        /// <summary>
        /// 根据当前请求的URL来取相应控件的路径
        /// </summary>
        /// <param name="url">当前请求的URL</param>
        public void AnalysisUrl(string url)
        {
            //读取模板信息,取出所有控件路径列表
            IList<PageInfo> pages = TemplateConfig.Pages;
            foreach (PageInfo page in pages)
            {
                if (Regex.IsMatch(url, page.PageName, RegexOptions.IgnoreCase))
                {//找到匹配项则跳出
                    //如果当前请求的URL在配置文件中则取相应控件的路径
                    this.TeamplateFilePath = GetControlPath(page.ControlPath);
                    break;
                }
            }
        }

        private string GetControlPath(string controlName)
        {
            ThemeReposity tr = new ThemeReposity();
            Theme theme = tr.FindBy("IsDefault", true);
            return ThemeCommon.GetControlPath(theme.FilePath, controlName);
        }
    }
}